package com.spedencencies.springjdbc2;

import com.spedencencies.springjdbc2.model.User;
import com.spedencencies.springjdbc2.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class SpringJdbc2Application implements CommandLineRunner{
    
    @Autowired
    private UserRepository userRepository;
    
    
    public static void main(String[] args) {
        SpringApplication.run(SpringJdbc2Application.class, args);
    }
    
    
    @Override
    public void run(String... args) {
        // 添加用户
        User user = new User();
        user.setName("一周一志");
        user.setEmail("zhizhou@example.com");
        user.setAge(18);
        userRepository.addUser(user);
        
        // 查询用户
        User queriedUser = userRepository.getUserById(3);
        System.out.println("Queried User: " + queriedUser.getName());
        
        // 更新用户
        queriedUser.setName("一周一志程序员 Updated");
        userRepository.updateUser(queriedUser);
        
        // 查询所有用户
        List<User> allUsers = userRepository.getAllUsers();
        allUsers.forEach(u -> System.out.println("User: " + u.getName()));
        
        // 删除用户
        //userRepository.deleteUser(2);
        
        // 测试其他查询方法
        userRepository.queryForRowSetExample();
    
        // 批量插入用户
        List<User> users = new ArrayList<>();
        User user1 = new User();
        user1.setName("一周一志");
        user1.setEmail("zhizhou@example.com");
        user1.setAge(18);
        users.add(user1);
        User user2 = new User();
        user2.setName("一周一志");
        user2.setEmail("1184795629@qq.com");
        user2.setAge(18);
        users.add(user2);
        int[][] res = userRepository.batchInsertUsers(users);
        //打印成json字符串
        for (int i = 0; i < res.length; i++) {
            for (int j = 0; j < res[i].length; j++)
                System.out.println("res[" + i + "][" + j + "] = " + res[i][j]);
        }
    }
}
